<#import "/spring.ftl" as spring />
<#import "html-macro.ftl" as h />
<#import "declaration-macro.ftl" as d />
<#assign hasAttachMode=false />
<#if attachState??>
<#assign hasAttachMode=true>
</#if>
<#if d.isSupplierPage__>
<script type="text/javascript" src="<@spring.url "/js/gtimp3/declaration.js"/>"></script>
<script src="/js/jw/jw.js"></script>
<div class="row" style="padding-left: 20px;">
    <div class="column">
        <#--<button id="returnBtn" type="button" class="tiny ui labeled icon button blue">-->
            <#--<i class="fa-arrow-left icon"></i>-->
            <#--返回-->
        <#--</button>-->
        <#if '${declaration.state}'=="DS01">
            <button id="toValidBtn" class="tiny ui labeled icon button blue">
                <i class="plus icon"></i>
                受理
            </button>
            <button id="toRejectBtn" class="tiny ui labeled icon button blue">
                <i class="save icon"></i>
                驳回
            </button>
            <#if isSignWS??>
                <button id="changeDataTypeBtn" class="tiny ui labeled icon button blue" style="display:none;">
                    <i class="small edit icon"></i>
                    【直/转类型】
                </button>
                <button id="confirmBGWInfo" class="tiny ui labeled icon button blue" style="display: none">
                    <i class="small send icon"></i>
                    确认三联单
                </button>
            </#if>
        </#if>
        <button id="uploadDeclarationAttachmentBtn" class="tiny ui labeled icon button blue">
            <i class="small download icon"></i>
            报关资料下载
        </button>
        <#if '${declaration.state}'=="DS02">
            <button id="changeDataTypeBtn" class="tiny ui labeled icon button blue">
                <i class="small download icon"></i>
                【直/转类型】
            </button>
        </#if>

        <#if isSecondSup && (declaration.state=='DS02' || declaration.state=="DS04" || declaration.state=="DS07")>
            <button id="confirmReleaseBtn" class="tiny ui labeled icon button blue">
                <i class="small download icon"></i>
                【确认放行】
            </button>
        </#if>

    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $(".button-line").remove();
        $goodsForm.show();
        $("#toValidBtn").click(function () {
            //通关一体化，选择第二供应商
            if ($("#isTongguanYth").val() == "1") {
                var index = layer.open({
                                           type: 2,
                                           title: '第二供应商选择',
                                           area: ['400px', '300px'],
                                           content: ['../business/selSecondSup?code=${declaration.export_port_code}',
                                                     'no'],
                                           btn: ['确定', '关闭'],
                                           yes: function (index, layero) {
                                               var selectObj = layer.getChildFrame('select', index);
                                               var $obj = $(selectObj);
                                               var secondSupId = $obj.find("option:selected")
                                                       .text();
                                               var secondSupIdCode = $obj.val();
                                               //alert($obj.find("option:selected").text()+$obj.val());

                                               var url = "/business/doSecondSupValid";
                                               var param = {};
                                               param.id = '${declaration.id}';
                                               param.secondSupId = secondSupId;
                                               param.secondSupIdCode = secondSupIdCode;

                                               $.ajax({
                                                          type: "POST",
                                                          url: url,
                                                          data: param,
                                                          success: function (r) {
	                                                          layer.close(index);
	                                                          if(r.result){
		                                                          //询问框
																	layer.confirm('受理成功', {
																	  btn: ['确定'] //按钮
																	}, function(){
																	  window.location.href ='../business/toValidMain';
																	});
	                                                           }else{
	                                                           	   layer.msg(r.msg);
	                                                           }
                                                          }
                                                      });

                                           },
                                           success: function (layero, index) {
                                               layer.iframeAuto(index);
                                           },
                                           cancel: function (index) {

                                           }
                                       });
            } else {
                ajaxGet('<@spring.url "../business/doValid?id=${declaration.id}"  />',
                        function (result) {
                            layer.alert(result.msg, function (index) {
                                layer.close(index);
                                <#if isSignWS??>
                                    //是签约用户, 页面停留, 显示【直/转类型】和【确认三联单】按钮
                                    $("#toValidBtn").attr("disabled", "disabled");
                                    $("#toRejectBtn").attr("disabled", "disabled");
                                    $("button[_id=toValidBtn]").attr("disabled", "disabled");
                                    $("button[_id=toRejectBtn]").attr("disabled", "disabled");
                                    $("#changeDataTypeBtn").show();
                                    $("button[_id=confirmBGWInfo]").show();
                                    $("#confirmBGWInfo").bind('click',function(){
                                        ajaxGet('<@spring.url "/business/confirmSanLianDan?ids=${declaration.id}"/>', function(data){
                                            if(data.result){
                                                $("#confirmBGWInfo").attr("disabled", "disabled");
                                            }
                                            layer.msg(data.msg);
                                        });
                                    }).show();
                                    $("button[_id=confirmBGWInfo]").bind('click',function(){
                                        ajaxGet('<@spring.url "/business/confirmSanLianDan?ids=${declaration.id}"/>', function(data){
                                            if(data.result){
                                                $("button[_id=confirmBGWInfo]").attr("disabled", "disabled");
                                            }
                                            layer.msg(data.msg);
                                        });
                                    }).show();
                                    <#else>
                                        window.location.href = '../business/toValidMain';
                                </#if>
                            });
                        });
            }
        });
        $("#toRejectBtn").click(function () {
            var url = "<@spring.url "../business/toReject?id=${declaration.id}"  />";
            var iWidth = 500;
            var iHeight = 300;
            var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
            var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
            var win = window.open(url, "弹出窗口",
                                  "width=" + iWidth + ", height=" + iHeight + ",top=" + iTop
                                  + ",left=" + iLeft
                                  + ",toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no,alwaysRaised=yes,depended=yes");

        });
        //确认放行
        $("#confirmReleaseBtn").click(function () {
            var okType = '海关放行';
            var id = '${declaration.id}';
            layer.confirm("确认完成" + okType + "?", function () {
                var url = "/business/doDeclarationOk?id=" + id + "&okType=" + okType;
                $.get(url, function (result) {
                    layer.msg(result.msg);
                });
            });
        });

        //查看编辑报关单资料
        var bill_no = '${declaration.id}';
        $("#uploadDeclarationAttachmentBtn").click(function () {
            if (bill_no == '') {
                bill_no = generateUUID();
                $("input[name='tempAttachmentBillNo']").val(bill_no);
            }
            layer.open({
                           type: 2,
                           offset: ['5%', '12%'],
                           content: '/business/uploadMain?no-upload=1&hide=1&type=declaration#'
                                    + bill_no,
                           area: ['815px', '620px']
                       });
        });

        $("#changeDataTypeBtn").click(function () {
            var url = "<@spring.url "/business/declaration-toZhuanGuan#${declaration.id}"  />";

            var iWidth = 500;
            var iHeight = 300;
            var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
            var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
            var win = window.open(url, "弹出窗口",
                                  "width=" + iWidth + ", height=" + iHeight + ",top=" + iTop
                                  + ",left=" + iLeft
                                  + ",toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no,alwaysRaised=yes,depended=yes");
        });
        $("button[_id=changeDataTypeBtn]").click(function () {
            var url = "<@spring.url "/business/declaration-toZhuanGuan#${declaration.id}"  />";

            var iWidth = 500;
            var iHeight = 300;
            var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
            var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
            var win = window.open(url, "弹出窗口",
                    "width=" + iWidth + ", height=" + iHeight + ",top=" + iTop
                    + ",left=" + iLeft
                    + ",toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no,alwaysRaised=yes,depended=yes");
        });
    });
</script>
<#else >
<div class="row">
    <div class="column">
        <#--<button id="returnBtn" class="tiny ui labeled icon button blue">-->
            <#--<i class="small fa-arrow-left icon"></i>-->
            <#--返回-->
        <#--</button>-->
        <button id="copyBtn" class="tiny ui labeled icon button blue">
            <i class="small copy icon"></i>
            复制
        </button>
        <#if d.isDraft__>
            <button id="editBtn" class="tiny ui labeled icon button blue">
                <i class="fa-pencil-square-o icon"></i>
                修改
            </button>
            <button id="submitBtn" class="tiny ui labeled icon button blue">
                <i class="small fa-check icon"></i>
                提交
            </button>
        </#if>
        <#if d.isOrder__ && declaration.payment_type != "1">
            <button id="payBtn" class="tiny ui labeled icon button blue">
                <i class="small copy icon"></i>
                支付
            </button>
        </#if>
        <#if !d.isSupplierPage__ && '${declaration.payment_type}' != "0" && '${declaration.state}' != "DS03"
        && '${declaration.state}' != "DS021" && '${declaration.state}' != "DS031" && '${declaration.state}' != "DS13">
            <button id="rejectBtn" class="tiny ui labeled icon button blue">
                <i class="fa-pencil-square-o icon"></i>
                申请驳回
            </button>
        </#if>
        <#if declaration.payment_type == "1" >
	         <#if hasAttachMode==true>
		        <button id="uploadDeclarationAttachmentBtn" class="tiny ui labeled icon button blue">
		            <i class="small download icon"></i>
		            报关资料下载
		        </button>
	        <#else>
		         <button id="uploadDeclarationAttachmentBtn" class="tiny ui labeled icon button while">
		            <i class="small download icon"></i>
		            报关资料下载
		        </button>
		    </#if>
		   </#if>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $goodsForm.show();
    });
</script>
</#if>
<script type="text/javascript">
    var rejectWin;
    $(document).ready(function () {
    <#if d.isDraft__>
        $("#editBtn").click(function () {
            location.href = "<@spring.url "/order/update" />?id=${declaration.id}";
        });
    </#if>
    <#if d.isOrder__>
        $("#payBtn").click(function () {
            location.href = "<@spring.url "/order/payPage" />?id=${declaration.id}";
        });
    </#if>

    });
    $("#returnBtn").click(function (event) {
        if (rejectWin) {
            rejectWin.close();
        }
        /*$("iframe").eq(0).css("display", "none");*/
        history.go(-1);
        /*window.location.href = "/business/validMain";*/
    });
    $("#rejectBtn").click(function () {
        $.ajax({
                   url: "/datalock/tryLock?opaId=VDBS&bidId=${declaration.id}",
                   success: function (data) {
                       if ("200" == data.errcode) {
                           var url = "<@spring.url "/business/reject/customer/page?declaId=${declaration.id}"  />";
                           var iWidth = 500;
                           var iHeight = 230;
                           var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
                           var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
                           rejectWin = window.open(url, "弹出窗口",
                                                   "width=" + iWidth + ", height=" + iHeight
                                                   + ",top=" + iTop
                                                   + ",left=" + iLeft
                                                   + ",toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no,alwaysRaised=yes,depended=yes");
                           var loop = setInterval(function () {
                               if (rejectWin.closed) {
                                   clearInterval(loop);
                                   /*location.reload();*/
                                   history.go(-1);
                               }
                           }, 300);
                       } else {
                           alert("报关单审核中，暂时不能操作");
                       }
                   }
               });
    });

		<#if hasAttachMode==true>
        //查看编辑报关单资料
        var bill_no = '${declaration.id}';
        $("#uploadDeclarationAttachmentBtn").click(function () {
            if (bill_no == '') {
                bill_no = generateUUID();
                $("input[name='tempAttachmentBillNo']").val(bill_no);
            }
            layer.open({
                           type: 2,
                           offset: ['5%', '12%'],
                           content: '/business/uploadMain?no-upload=1&hide=1&type=declaration#'
                                    + bill_no,
                           area: ['815px', '620px']
                       });
        });
        </#if>
</script>
